package com.xm;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @Author：xm
 * @Project：runoob
 * @Date：2024/7/15 22:26
 */

public class Serializable07 {
    public static void main(String[] args) {

        Serializable072 serializable072 = new Serializable072();
        serializable072.name = "xm";
        serializable072.address = "china";
        serializable072.SSN = 369;
        serializable072.number = 159;

        // 序列化
        try {
            // serializable072.ser 在runoob路径下 /serializable072.ser在D盘路径下
            FileOutputStream fileOutputStream = new FileOutputStream("serializable072.ser");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(serializable072);

            // 基本数据类型序列化，反序列化需要顺序读取。
            // objectOutputStream.writeInt(333);
            // objectOutputStream.writeBoolean(true);

            objectOutputStream.close();
            fileOutputStream.close();
            System.out.printf("Serialized data is saved in serializable072.ser");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 反序列化
        // try {
        //     FileInputStream fileInputStream = new FileInputStream("serializable072.ser");
        //     ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
        // 顺序读取
        // System.out.println();
        // System.out.println(objectInputStream.readInt());
        // System.out.println(objectInputStream.readBoolean());

        // } catch (IOException e) {
        //     throw new RuntimeException(e);
        // }
    }
}
